package Api;

import java.util.ArrayList;
import java.util.Collection;

public class ListTest01 {
    public static void main(String[] args) {
        ArrayList<String> fruits = new ArrayList<>();

        // 添加元素
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
        fruits.add(0,"aaa");

        // 打印列表大小
        System.out.println("List size: " + fruits.size());

        // 检查列表是否包含某个元素
        if (fruits.contains("Banana")) {
            System.out.println("Banana is in the list.");
        }

        // 获取并打印指定索引处的元素
        String secondFruit = fruits.get(1);
        System.out.println("Second fruit is: " + secondFruit);

        // 在指定位置插入一个元素
        fruits.add(1, "Berry");
        System.out.println("List after insertion: " + fruits);

        // 替换指定位置的元素
        fruits.set(1, "Blackberry");
        System.out.println("List after replacement: " + fruits);
        // 删除指定位置的元素
        fruits.remove(1);
        System.out.println("List after deletion: " + fruits);
        // 清空列表
        fruits.clear();
        System.out.println("List is empty now? " + fruits.isEmpty());
    }
}
